package com.joyoung.smartrobotcommon;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.joyoung.common.L;
import com.joyoung.common.api.ApiManager;
import com.joyoung.common.api.BaseResponse;
import com.joyoung.smartrobotcommon.api.JYApi;
import com.joyoung.smartrobotcommon.api.RequestParam;
import com.joyoung.smartrobotcommon.api.RequestParam2;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

public class RequestActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String TAG = "BBB";
    JYApi jyApi;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        findViewById(R.id.button1).setOnClickListener(this);
        
        //api 获取
        jyApi = ApiManager.getInstance().getApi(JYApi.class);
        
        
   
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                test1();
                test2();
                break;
        }
    }
    
    private void test2() {
        RequestParam2 param = new RequestParam2();
        
        jyApi.test2(param.param,param.devTypeId,param.stamp)//调用请求方法
                .subscribeOn(Schedulers.io())//在异步线程中执行
                .observeOn(AndroidSchedulers.mainThread())//切换回主线程
                .subscribe(new DisposableSubscriber<BaseResponse<Object>>() {//回调
                    @Override
                    public void onNext(BaseResponse<Object> result) {//成功
                        L.f().i(TAG, "onNext");
                    }
                
                    @Override
                    public void onError(Throwable t) {//失败
                        L.f().i(TAG, "onError");
                    }
                
                    @Override
                    public void onComplete() {//完成
                        L.f().i(TAG, "onComplete");
                    }
                });
    }
    
    private void test1() {
        RequestParam param = new RequestParam();
        jyApi.test1(param).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableSubscriber<BaseResponse<Object>>() {
                    @Override
                    public void onNext(BaseResponse<Object> result) {
                        L.f().i(TAG, "onNext");
                    }
                    
                    @Override
                    public void onError(Throwable t) {
                        L.f().i(TAG, "onError");
                    }
                    
                    @Override
                    public void onComplete() {
                        L.f().i(TAG, "onComplete");
                    }
                });
    }
}
